package com.epf.sso.server.service.impl;

import com.epf.sso.dao.UserInfoDao;
import com.epf.sso.enums.LoginMessageEnums;
import com.epf.sso.server.core.model.UserInfo;
import com.epf.sso.server.core.result.ReturnT;
import org.apache.shiro.crypto.hash.Sha256Hash;

import com.epf.sso.server.service.UserService;
import com.tiaim.framework.utils.web.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoDao userInfoDao;

    private static List<UserInfo> mockUserList = new ArrayList<>();
    static {
        for (int i = 0; i <5; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(1000+i+"");
            userInfo.setUsername("user" + (i>0?String.valueOf(i):""));
            userInfo.setPassword("123456");
            mockUserList.add(userInfo);
        }
    }

    @Override
    public ReturnT<UserInfo> findUser(String username, String password) {

        if (username==null || username.trim().length()==0) {
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "Please input username.");
        }
        if (password==null || password.trim().length()==0) {
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "Please input password.");
        }

        // mock user
        //TODO zhouxb 登录
        /*for (UserInfo mockUser: mockUserList) {
            if (mockUser.getUsername().equals(username) && mockUser.getPassword().equals(password)) {
                return new ReturnT<UserInfo>(mockUser);
            }
        }*/

        try{
            UserInfo user = getLoginUser( username,  password);
            return new ReturnT<UserInfo>(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "username or password is invalid.");
    }

    /**
     *  获取登录的用户
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    public UserInfo getLoginUser(String username, String password) throws Exception{
            // 检验用户名是否存在
            UserInfo user = queryUserByUsername(username, "0");
            if (user == null) {
                throw new Exception(LoginMessageEnums.LOGIN_ACCOUNT_OR_PASSWORD_ERROR.getMsg()) ;
            } else {
                // 检验密码是否正确
                String password_cipherText = new Sha256Hash(password.trim(), user.getSalt()).toHex();
                if (password_cipherText.equals(user.getPassword())) {
                    user.setPassword("******");// 置空密码
                    if (("2".equals(user.getUserstate()))) { // 如果状态审核中
                        throw new Exception(LoginMessageEnums.USER_IS_IN_AUDIT.getMsg());
                    } else if (("3".equals(user.getUserstate()))) { // 如果状态审核中
                        throw new Exception(LoginMessageEnums.USER_IS_OUT_AUDIT.getMsg());
                    }
                    return user;
                } else {
                    throw new Exception(LoginMessageEnums.LOGIN_ACCOUNT_OR_PASSWORD_ERROR.getMsg());
                }
            }
    }

    /**
     * 通过用户账号查找用户
     *
     * @param username
     * @return
     */
    public UserInfo queryUserByUsername(String username, String loginType) {
        UserInfo vuser = new UserInfo();
        vuser.setUsername(username.trim());
        vuser.setDeleteType("0");
        vuser.setUsetype(loginType);
        UserInfo vus = userInfoDao.selectOne(vuser);
        return vus;
    }
}
